﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WarehouseManager.DataAccess;
using WarehouseManager.Model;

namespace WarehouseManager.View
{
    public partial class FormXuatHang : Form
    {
        private int sum1,max;
        private string s;
        ThongkeAccess thongkeAccess = new ThongkeAccess();
        NhanvienAccess nhanvienAccess = new NhanvienAccess();
        KhachhangAcess khachhangAccess = new KhachhangAcess();
        HanghoaAccess hanghoaAccess = new HanghoaAccess();
        PhieuxuatAccess phieuxuatAccess = new PhieuxuatAccess();
        CTPhieuxuatAccess ctphieuxuatAccess = new CTPhieuxuatAccess();

        public FormXuatHang(string s) 
        {
            this.s = s;
            if (s != null)
            {

                InitializeComponent(0);
            }
            else
            {

                InitializeComponent(1);
            }
            
        }
       
        private void cbMakh_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = khachhangAccess.Laytoanbokh();
            DataRow dataRow;
            dataRow = dt.Rows[cbMakh.SelectedIndex];
            txtHotenkh.Text = dataRow["hodemKH"].ToString() + " " + dataRow["tenKH"].ToString();
            txtDiachikh.Text = dataRow["diaChi"].ToString();
            txtSdtkh.Text = dataRow["soDT"].ToString();
        }
        private void cbManv_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt = nhanvienAccess.Laytoanbonhanvien();
            DataRow dataRow;
            dataRow = dt.Rows[cbManv.SelectedIndex];
            txtHotennv.Text = dataRow["hodemNV"].ToString() + " " + dataRow["tenNV"].ToString();
            txtGioitinhnv.Text = dataRow["gioiTinh"].ToString();
            txtSdtnv.Text = dataRow["soDT"].ToString();
        }
        private void loadNhanvien()
        {
            DataTable dt = new DataTable();
            dt = nhanvienAccess.Laytoanbonhanvien();
            DataRow dataRow;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dataRow = dt.Rows[i];
                this.cbManv.Items.Add(dataRow["maNV"].ToString());
            }
        }
        private void loadNhacungcap()
        {
            DataTable dt = new DataTable();
            dt = khachhangAccess.Laytoanbokh();
            DataRow dataRow;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dataRow = dt.Rows[i];
                cbMakh.Items.Add(dataRow["maKH"].ToString());

            }
        }
        private void loadKhachhang()
        {
            DataTable dt = new DataTable();
            dt = khachhangAccess.Laytoanbokh();
            DataRow dataRow;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dataRow = dt.Rows[i];
                this.cbMakh.Items.Add(dataRow["maKH"].ToString());
            }
        }
        private void buttThoat_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn thoát không ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1) == DialogResult.OK)
            {
                this.Close();
            }
        }
        private void FormXuatHang_Load(object sender, EventArgs e)
        {
            sum1 = 0;
            max = 0;
            if (s == null)
            {
                
                loadNhanvien();
                layMaphieunhap();
                loadMahang();
                loadKhachhang();
                layNgaynhap();
            }
            else
            {
                cbMakh.Enabled = true;
                cbManv.Enabled = true;
                dataGridView1.ReadOnly = true;
                loadAll();
                loadDataGridview();
            }
        }
        public void layNgaynhap()
        {
            dtNgaylapphieu.Text = DateTime.Now.ToString();
        }
        private void buttThemkh_Click(object sender, EventArgs e)
        {
            FormNhanvien nv = new FormNhanvien();
            nv.Show();
            FormXuatHang_Load(sender, e);
        }

        
        public void layMaphieunhap()
        {
            LayMa layma = new LayMa();
            DataTable dt = new DataTable();
            dt = phieuxuatAccess.laytoanbophieuxuat();
            DataRow dataRow = dt.Rows[(dt.Rows.Count - 1)];
            txtMaphieu.Text = layma.NextIDPX(dataRow["maPX"].ToString());
        }
        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            using (SolidBrush b = new SolidBrush(dataGridView1.RowHeadersDefaultCellStyle.ForeColor))
            {
                e.Graphics.DrawString(e.RowIndex.ToString(), dataGridView1.DefaultCellStyle.Font, b, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4);
            }
        }

        public void loadMahang()
        {
            DataTable dataTable = new DataTable();
            dataTable = hanghoaAccess.layToanbohanghoa();
            for (int i = 0; i < dataTable.Rows.Count; i++)
            {
                DataRow dr = dataTable.Rows[i];
                Column2.Items.Add(dr["maHH"].ToString());
            }
        }
        private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
            if (dataGridView1.CurrentCell.ColumnIndex == 0)
            {
                ComboBox column2 = e.Control as ComboBox;
                column2.SelectedIndexChanged += new EventHandler(cbColumn2_SelectedIndexChanged);
            }
        }
        private void cbColumn2_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedIndex = ((ComboBox)sender).SelectedIndex;
            DataTable dt = new DataTable();
            dt = hanghoaAccess.layToanbohanghoa();
            DataRow dataRow,dr;
            dataRow = dt.Rows[selectedIndex];
            dataGridView1.CurrentRow.Cells[1].Value = dataRow["tenHH"].ToString();
            dataGridView1.CurrentRow.Cells[3].Value = dataRow["donViTinh"].ToString();
            dataGridView1.CurrentRow.Cells[4].Value = dataRow["donGia"].ToString();
            DataTable dt2 = thongkeAccess.layThongtinkhotheomahh(dataRow["maHH"].ToString());
            if (dt2.Rows.Count>0)
            {
                dr = dt2.Rows[0];
                max = int.Parse(dr["soLuong"].ToString());
            }
        }

        private void buttThem_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
            {
                string s1 = dataGridView1.Rows[i].Cells[0].Value.ToString();
                string s2 = dataGridView1.Rows[i].Cells[2].Value.ToString();
                Phieuxuat pn = new Phieuxuat(txtMaphieu.Text, cbMakh.Text, DateTime.Parse(dtNgaylapphieu.Text), cbManv.Text);
                phieuxuatAccess.themPhieuxuat(pn);
                CTPhieuxuat ctpx = new CTPhieuxuat(txtMaphieu.Text, s1, int.Parse(s2));
                ctphieuxuatAccess.themCTPhieuxuat(ctpx);

            }
            MessageBox.Show("Bạn đã thêm thành công!");
        }

        
        private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int t1=0, t2=0;

            if (dataGridView1.CurrentCell.ColumnIndex == 2)
            {
                t1 = int.Parse(dataGridView1.CurrentRow.Cells[2].Value.ToString());
                t2 = int.Parse(dataGridView1.CurrentRow.Cells[4].Value.ToString());
                sum1 += (t1 * t2) / 2;
                dataGridView1.CurrentRow.Cells[5].Value = (t1 * t2).ToString();
                
            }
            if (t1 > max) MessageBox.Show("Bạn số lương không vượt quá " + max.ToString() + "!");
            label21.Text = sum1.ToString() + " " + "(vnđ)";
            
        }
        private void loadAll()
        {
            DataTable dt = new DataTable();
            dt = phieuxuatAccess.layView(s);
            txtMaphieu.Text = dt.Rows[0]["maPX"].ToString();
            dtNgaylapphieu.Text = dt.Rows[0]["ngayXuat"].ToString();
            cbMakh.Text = dt.Rows[0]["maKH"].ToString();
            txtHotenkh.Text = dt.Rows[0]["tenKH"].ToString();
            txtSdtkh.Text = dt.Rows[0]["soDTkh"].ToString();
            txtDiachikh.Text = dt.Rows[0]["diaChikh"].ToString();
            cbManv.Text = dt.Rows[0]["maNV"].ToString();
            txtHotennv.Text = dt.Rows[0]["hoten"].ToString();
            txtGioitinhnv.Text = dt.Rows[0]["gioiTinh"].ToString();
            txtSdtnv.Text = dt.Rows[0]["soDTnv"].ToString();
        }
        private void loadDataGridview()
        {
            int t1, t2, sum = 0;
            DataTable dt = phieuxuatAccess.layHanghoatheoMapx(s);
            dataGridView1.DataSource = dt;
            DataRow dr;
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                dr = dt.Rows[i];
                t1 = int.Parse(dr["donGia"].ToString());
                t2 = int.Parse(dr["soLuong"].ToString());
                sum += (t1 * t2);
                dataGridView1.Rows[i].Cells[0].Value = (t1 * t2).ToString();
            }
            label21.Text = sum.ToString() + " (vnđ)";
        }
    }
}
